<?
/**
 * This file is part of XNAT light.
 *
 * XNAT light is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XNAT light is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XNAT light.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace NRG\Xnat;

use NRG\Database\Mappers as Mappers;
use NRG\Framework as Framework;

/**
 * XNAT project model
 *
 * @package NRG\Xnat
 */
class Project extends XObject
{
    /**
     * Project ID
     *
     * @var string
     */
    protected $_id;

    /**
     * Project name
     * 
     * @var string
     */
    protected $_name;

    /**
     * Project description
     *
     * @var string
     */
    protected $_description;

    /**
     * Project archive path
     *
     * @var string
     */
    protected $_archivePath;

    /**
     * Subjects
     *
     * @var array (string, NRG\Xnat\Subject)
     */
    protected $_subjects;

    /**
     * Constructor
     */
    public function __construct()
    {
	parent::__construct();

	$this->_subjects = array();
    }

    /**
     * Set project ID
     *
     * @param string $id
     */
    public function setID($id=null)
    {
	if(!is_string($id))
	    throw new Framework\Exceptions\IllegalArgumentException("ID must be a string");
	else if($id == "")
	    throw new Framework\Exceptions\IllegalArgumentException("ID cannot be null");

	$this->_id = $id;
    }

    /**
     * Access project ID
     *
     * @return string
     */
    public function getID()
    {
	return $this->_id;
    }

    /**
     * Set project name
     *
     * @param string $name
     */
    public function setName($name=null)
    {
	$this->_name = $name;
    }

    /**
     * Access project name
     *
     * @return string
     */
    public function getName()
    {
	return $this->_name;
    }

    /**
     * Access project name
     *
     * @return string
     */
    public function getProjectName()
    {
	return $this->_name;
    }

    /**
     * Access this project
     *
     * @return NRG\Xnat\Project
     */
    public function getProject()
    {
	return $this;
    }

    /**
     * Set project description
     *
     * @param string $description
     */
    public function setDescription($description=null)
    {
	$this->_description = $description;
    }

    /**
     * Access project description
     *
     * @return string
     */
    public function getDescription()
    {
	return $this->_description;
    }

    /**
     * Access the archive path
     *
     * @return string
     */
    public function getArchivePath()
    {
	return $this->_archivePath;
    }

    /**
     * Set project archive path
     *
     * @return string $path
     */
    public function setArchivePath($archivePath=null)
    {
	$this->_archivePath = $archivePath;
    }

    /**
     * Add a subject ID to this project
     *
     * @param string $id
     * @param \NRG\Xnat\Subject $subject
     */
    public function addSubject($id=null, Subject $subject=null)
    {
	if(!is_string($id))
	    throw new Framework\Exceptions\IllegalArgumentException("Subject ID must be a string");
	else if($id == null)
	    throw new Framework\Exceptions\IllegalArgumentException("Subject ID cannot be null");

	$this->_subjects[$id] = $subject;
    }

    /**
     * Access a subject by ID
     *
     * @param string $key
     * @return NRG\Xnat\Subject
     */
    public function getSubject($key=null)
    {
	if(!is_string($key))
	    throw new Framework\Exceptions\IllegalArgumentException("Subject ID must be a string");
	else if($key == "")
	    throw new Framework\Exceptions\IllegalArgumentException("Subject ID cannot be null");

	if(!array_key_exists($key, $this->_subjects))
	    throw new Framework\Exceptions\IndexOutOfBoundsException($key);

	if($this->_subjects[$key] === null)
	    $this->getSubjects();

	return $this->_subjects[$key];
    }

    /**
     * Access all subjects
     *
     * @return array (int, NRG\Xnat\Subject)
     */
    public function getSubjects()
    {
	if(current($this->_subjects) == null)
	{
	    $mapper = new Mappers\Subject();
	    $this->_subjects = $mapper->byList(array_keys($this->_subjects));
	}

	return $this->_subjects;
    }

    /**
     * Access Subject IDs
     *
     * @return array (int, string)
     */
    public function getSubjectIDs()
    {
	return array_keys($this->_subjects);
    }
}
